Skip to main content
Introduction

A lot of resources drain your CPU usage by using "old" ways of checking distance between coords. The solution is easy!

Optimalization

If you will follow these steps you will be fine

  1. Use Lua math, functions ... instead of FiveM natives/functions
  2. Use FiveM functions/Natives instead of your framework functions
  3. Use your framework functions

How does that work? It is easy, just go 1 - 3, if you can not use 1., use the 2., if you can not use the point 3. Why? The first is the faster one and the 3. is the slowest one in the most cases. Why to use framework function if raw Lua can do it?

Counting Distance about 2 points

This seems to be the biggest problem on most servers? Why? It is so f*ckin' easy to optimise getting distance between 2 places.

Citizen.CreateThread(function ()
while true do
Wait(0)

local coords = GetEntityCoords(GetPlayerPed(-1))

for k,v in pairs(Config.Zones) do
if(v.Type ~= -1 and GetDistanceBetweenCoords(coords, v.Pos.x, v.Pos.y, v.Pos.z, true) < Config.DrawDistance) then
DrawMarker(v.Type, v.Pos.x, v.Pos.y, v.Pos.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, v.Size.x, v.Size.y, v.Size.z, v.Color.r, v.Color.g, v.Color.b, 100, false, true, 2, false, false, false, false)
end
end
end
end)

-- Lets say that we have defined all the locations and the Config with DrawDistance
-- It is incorrect, because "GetDistanceBetweenCoords" is "slow" in comparison with lua math